//
//  EquipmenDetailsViewModel.m
//  DataStatistics
//
//  Created by Kang on 15/12/30.
//  Copyright © 2015年 YTYangK. All rights reserved.
//

#import "EquipmenDetailsViewModel.h"
#import "DeviceDetailsModel.h"  //设备详情模型
#import "DeviceBindingModel.h"   // 添加设备模型

@interface EquipmenDetailsViewModel()
@end


@implementation EquipmenDetailsViewModel
singleton_for_implementation(EquipmenDetailsViewModel)


#pragma mark - 扫描设备的详情信息
+ (void)requestWithUrlForDeviceDetails:(id)obj andParam:(NSDictionary *)param
                               success:(void(^)(DeviceDetailsModel *model))success failure:(void(^)(NSString * error))failure {
    //__weak typeof (ViewModelClass *) weakSuper = [ViewModelClass sharedViewModelClss];
       [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    [NetRequestClss requestWithUrl:@"deviceList/querDetailList.asp" requestWithParameters:param method:NetMethodGET returnSuccess:^(id objs, int status, NSString *mag) {
        DeviceDetailsModel *listModel = [DeviceDetailsModel mj_objectWithKeyValues:[objs objectForKey:@"body"]];
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
      //  [[UtilToolsClss getUtilTools] removeDoLoading];

        NSLog(@"listModel---> %@",listModel);
        if (![listModel isEqual:[NSNull null]]) {
            success(listModel);
            return;
        }
        failure(@"暂无数据");
        
    } returnError:^(NSString *err) {
        failure(err); //返回是因为需要列表拉下来
    }];
}


#pragma mark - 添加绑定 && 修改昵称
+ (void)requestWithUrlForDeviceBinding:(id)obj andParam:(NSDictionary *)param
                               success:(void(^)(id model))success failure:(void(^)(NSString * error))failure {
    NSMutableDictionary *mutDic = [[NSMutableDictionary alloc] init];
    NSString *halfPathUrl = [param objectForKey:@"querDetailList"];
    if (halfPathUrl == nil) {
        halfPathUrl = @"deviceList/deviceBinding.asp";
        [mutDic setDictionary:param];
    }else {
        halfPathUrl = [NSString stringWithFormat:@"deviceList/%@",halfPathUrl];
        [mutDic setObject:[param objectForKey:@"deviceNo"] forKey:@"deviceNo"];
        [mutDic setObject:[param objectForKey:@"nickname"] forKey:@"nickname"];
    }
    MDC_INTER_DIC(halfPathUrl, @"NetMethodPOST", @"false")
    [super initRequestWithUrlObj:obj parametersDic:param Interface:inter success:^(id obj, id model) {
        NSString *tipsStr = @"";
        if ([halfPathUrl rangeOfString:@"nickname.asp"].location == NSNotFound) {
            DeviceBindingModel* binding = [[DeviceBindingModel alloc] initWithDictionary:model error:nil];
            DeviceBindingList *list = binding.DeviceBindingList[0];
            
            if (list.result != 1) {
                if (list.result == 0) {
                    tipsStr = list.error;
                }else if (list.result == 2){
                    tipsStr =  NSLocalizedString(@"SysTip_1", @"已绑定");
                }else if(list.result == 3) {
                    tipsStr = NSLocalizedString(@"SysTip_2", @"空间不足");
                }else if (list.result == 4) {
                    tipsStr = NSLocalizedString(@"SysTip_3", @"无法绑定");
                }else {
                    tipsStr = NSLocalizedString(@"SysTip_4", @"");
                }
                failure(tipsStr);
            }else {
                success(list.deviceNo);
            }
        }else {
            if ([[[model objectForKey:@"body"] objectForKey:@"result"] intValue] == 1) {
                success(@"修改成功");
                return;
            }
            
            failure(@"修改失败");
        }
    } failure:^(id obj, NSString *err) {
          failure(err); //返回是因为需要列表拉下来
    }];

    
    
}



@end
